package com.ssm.zyn.service.impl;

import com.ssm.zyn.dao.StudentMapper;
import com.ssm.zyn.eo.StudentEOpage;
import com.ssm.zyn.model.Student;
import com.ssm.zyn.service.Studentservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实现业务及流程接口当中的内容
 */
@Service
public class StudentServiceImpl implements Studentservice {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 角色添加方法
     * @param student 角色实体
     * @return 是否成功
     */
    @Override
    public int AddtStudent(Student student) {
        int returnInt = 0;
        /**1业务判断
         * （1）学号不能重复
         */
        //（1）判断学号
        int stuidCount=studentMapper.getStuidCount(student.getStuid());
        if(stuidCount<1){
            //2.数据添加
            int addStudentInt=studentMapper.insertStudent(student);
            //3.返回值设置
            returnInt=addStudentInt;
        }else {
            returnInt=-1;//有相同的角色名称
        }

        return returnInt;
    }

    /**
     * 学生更新
     * @param student 学生实体
     * @return 更新是否成功（-1：更新执行SQL报错）
     */

    @Override
    public int UpdateStudent(Student student) {
        int isUpdata=0;
        int stuId=studentMapper.getStuidCount(student.getStuid());
        /*1.没有相关业务流程判断*/
        try{
            if (stuId<1) {
                isUpdata = studentMapper.updateStudent(student);
            }
        }catch (Exception e){
            isUpdata=-1;
            //打印更新报错的情况
            System.out.println("更新角色信息时执行SQL报错的原因为"+e.getMessage());
        }
        return isUpdata;
    }

    /**
     * 删除学生
     * @param sid
     * @return
     */

    @Override
    public int DeleteStudent(int sid) {
        int delStudent=0;
        try{
            delStudent=studentMapper.delStudent(sid);
        }catch (Exception e){
            delStudent=-1;
            System.out.println("删除学生信息时执行SQL报错，信息为"+e.getMessage());
        }
        return delStudent;
    }

    @Override
    public List<StudentEOpage> getWhereAll(StudentEOpage studentEOpage) {
        List<StudentEOpage> listStudent= studentMapper.getWhereAll(studentEOpage);
        return listStudent;
    }

}
